package cn.tuling.v2;

/**
 * 进步：
 * 1. 符合“单一职责原则”：Boy类负责追求女孩，Memento负责保留状态
 * 2. 封装性OK：高层模块不用关心备份的创建，由Boy类创建
 * 缺点：
 * 1. 不符合“迪米特法则”：高层模块仍需要知道Memento类，但其实他并不关心，高层模块想要的是：创建一个备份点，当我需要的时候帮我恢复
 */
public class MementoTest {
    public static void main(String[] args) {
        Boy boy = new Boy();
        boy.setState("You're so beau~beautiful.");
        System.out.println("初始状态");
        System.out.println(boy.getState());

        Memento memento = boy.createMemento();
        System.out.println("改变状态");
        boy.changeState();
        System.out.println(boy.getState());

        System.out.println("恢复状态");
        boy.restoreMemento(memento);
        System.out.println(boy.getState());
    }
}

class Memento {
    private String state;

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}

class Boy {
    private String state;

    public void changeState() {
        this.state = "You so bad bad.";
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Memento createMemento() {
        Memento memento = new Memento();
        memento.setState(state);
        return memento;
    }

    public void restoreMemento(Memento backupMemento) {
        this.setState(backupMemento.getState());
    }
}

